#/usr/bin/env bash

function __hms_options_completion ()
{
	local word="$1"
	if [[ "$word" == "-"* ]]; then
		if [[ "$word" == "-d" ]] || [[ "$word" == "--directory" ]]; then
			# return the directory option for compgen/complete
			echo "-d"
		elif [[ "$word" == "-l" ]] || [[ "$word" == "--log-level" ]]; then
			echo "-W trace debug info warn err critical off"
		elif [[ "$word" == "-i" ]] || [[ "$word" == "--import" ]]; then
			echo "-W all mesh dem roughness rainfall"
		fi
	fi
}

function __hms_completion ()
{
	local options="-h --help -d --directory -v --version -l --log-level -q --quiet -c --clear -m --mesh -i --import -n --dry-run"
	COMPREPLY=();
	local word="${COMP_WORDS[COMP_CWORD]}";

	if [ "$COMP_CWORD" -eq 1 ]; then
		COMPREPLY=($(compgen -d -W "$options" -- "$word"));
	else
		local words=("${COMP_WORDS[@]}");
		# I don't know what the unset commands are for.
		# Passing words[1] to __hms_options_completion would probably be enough.
		unset words[0];
		unset words[$COMP_CWORD];
		local completions="$(__hms_options_completion ${words[@]})";
		# If I want to expand completions into multiple arguments for compgen,
		# the " need to be removed. But then the other return values don't work.
        COMPREPLY=($(compgen "${completions[@]}" -- "$word"));
	fi
}

complete -F __hms_completion hms
